package com.ddlaw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ddlaw.entity.TLawyer;
import com.ddlaw.entity.TLawyerApplyCustom;
import com.ddlaw.entity.TLawyerDetailCustom;
import com.ddlaw.entity.TLawyerServiceType;
import com.ddlaw.entity.TProvince;
import com.ddlaw.entity.TStudentDetaiCustom;
import com.ddlaw.service.LawyerService;
import com.ddlaw.service.UserService;
import com.ddlaw.utils.Upload;
import com.ddlaw.utils.XmlReadUtil;

/**
 * 
 *@author YunLong
 *@time 下午8:45:25
 */
@Controller
@RequestMapping(value = "/lawyer")
public class LawyerController {
	
	@Resource
	UserService userService;
	@Resource
	LawyerService lawyerService;
	
	/**
	 * 待审核专业律师列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "auditWaitLawyer", method = RequestMethod.GET)
	public String auditWaitV(Model model) {
		List<TLawyerApplyCustom> lawyerDetail= lawyerService.getauditWaitLawyer();
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		for(TLawyerApplyCustom lawyer:lawyerDetail){
			lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
			lawyer.setSpecialNameB(specials.get(lawyer.getSpecialb()+""));
			lawyer.setSpecialNameC(specials.get(lawyer.getSpecialc()+""));
		}
		model.addAttribute("lawyers", lawyerDetail);
		return "lawyer/audit-wait-lawyer";
	}
	
	/***
	 * 其他律师申请列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "student/apply", method = RequestMethod.GET)
	public String studentApply(Model model) {
		List<TStudentDetaiCustom> lawyerDetail= lawyerService.getStudentApply();
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		for(TStudentDetaiCustom lawyer:lawyerDetail){
			lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
		}
		model.addAttribute("lawyers", lawyerDetail);
		return "lawyer/audit-wait-stu";
	}
	
	/**
	 * 专业律师已审核列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "auditLawyer", method = RequestMethod.GET)
	public String auditLawyersV(Model model) {
		List<TLawyerDetailCustom> lawyerDetail= lawyerService.getLawyers();
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		for(TLawyerDetailCustom lawyer:lawyerDetail){
			lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
			lawyer.setSpecialNameB(specials.get(lawyer.getSpecialb()+""));
			lawyer.setSpecialNameC(specials.get(lawyer.getSpecialc()+""));
		}
		List<TLawyerServiceType> serviceTypes = lawyerService.getServiceType();
		List<TProvince> provinces = lawyerService.getProvince();
		model.addAttribute("lawyers", lawyerDetail);
		model.addAttribute("serviceTypes", serviceTypes);
		model.addAttribute("provinces", provinces);
		return "lawyer/hire-lawyer";
	}
	
	/***
	 * 其他律师已审核列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "student/auditLawyer", method = RequestMethod.GET)
	public String auditStuLawyers(Model model) {
		List<TStudentDetaiCustom> stuDetail= lawyerService.getStus();
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		for(TStudentDetaiCustom lawyer:stuDetail){
			lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
		}
		List<TLawyerServiceType> serviceTypes = lawyerService.getServiceType();
		List<TProvince> provinces = lawyerService.getProvince();
		model.addAttribute("lawyers", stuDetail);
		model.addAttribute("serviceTypes", serviceTypes);
		model.addAttribute("provinces", provinces);
		return "lawyer/hire-stu";
	}
	
	/***
	 * 专业律师审核详情页
	 * @param applyId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "audit/{applyId}", method = RequestMethod.GET)
	public String auditV(@PathVariable(value = "applyId") int applyId , Model model) {
		TLawyerApplyCustom lawyerDetail= lawyerService.getAuditWaitLawyerById(applyId);
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		lawyerDetail.setSpecialNameA(specials.get(lawyerDetail.getSpeciala()+""));
		lawyerDetail.setSpecialNameB(specials.get(lawyerDetail.getSpecialb()+""));
		lawyerDetail.setSpecialNameC(specials.get(lawyerDetail.getSpecialc()+""));
		model.addAttribute("lawyers", lawyerDetail);
		return "lawyer/audit-lawyer";
	}
	
	/***
	 * 非专业律师审核详情页
	 * @param applyId 审核ID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "student/audit/{applyId}", method = RequestMethod.GET)
	public String auditStu(@PathVariable(value = "applyId") int applyId , Model model) {
		TStudentDetaiCustom stu= lawyerService.getStuById(applyId);
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		stu.setSpecialNameA(specials.get(stu.getSpeciala()+""));
		model.addAttribute("stu", stu);
		return "lawyer/audit-stu";
	}
	
	/**
	 * 专业律师详情
	 * @param lawyerId 律师ID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detail/{lawyerId}", method = RequestMethod.GET)
	public String detail(@PathVariable(value = "lawyerId") int lawyerId , Model model) {
		TLawyerDetailCustom lawyerDetail= lawyerService.getLawyerDetail(lawyerId);
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		lawyerDetail.setSpecialNameA(specials.get(lawyerDetail.getSpeciala()+""));
		lawyerDetail.setSpecialNameB(specials.get(lawyerDetail.getSpecialb()+""));
		lawyerDetail.setSpecialNameC(specials.get(lawyerDetail.getSpecialc()+""));
		model.addAttribute("lawyers", lawyerDetail);
		return "lawyer/lawyer-detail";
	}
	
	/***
	 * 其他律师详情
	 * @param lawyerId	律师ID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "student/detail/{lawyerId}", method = RequestMethod.GET)
	public String detailStu(@PathVariable(value = "lawyerId") int lawyerId , Model model) {
		TStudentDetaiCustom lawyerDetail= lawyerService.getStuDetail(lawyerId);
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		lawyerDetail.setSpecialNameA(specials.get(lawyerDetail.getSpeciala()+""));
		model.addAttribute("lawyers", lawyerDetail);
		return "lawyer/stu-detail";
	}
	
	/***
	 * 专业律师审核通过
	 * @param detail 律师详细信息
	 * @param applyId 申请编号ID
	 * @return
	 */
	@RequestMapping(value = "agree", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> agree(TLawyerDetailCustom detail, int applyId,HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		TLawyer lawyer = new TLawyer();
		lawyer.setName(detail.getName());
		lawyer.setSex(detail.getSex().charAt(0));
		lawyer.setState(0);
		lawyer.setType(0);
		lawyer.setUserId(detail.getUserid());
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		MultipartFile multipartFile = multipartRequest.getFile("file");
		//图片存储访问路径为 images
		String imgUrl = Upload.uploadFile(multipartFile, request, "images/",detail.getName());
		//这两个操作应该在一个事务中，这样写是有问题的，不在一个事务会出现错误
		int getIdRs = lawyerService.addAndGetId(lawyer);
		int delRs = lawyerService.deleteById(applyId);
		if (getIdRs > 0 && delRs > 0) {
			detail.setIdcardimg(imgUrl);
			detail.setLawyerid(lawyer.getLawyerId());
			int rs = lawyerService.addLawyer(detail);
			if (rs > 0) {
				int typeRs = userService.updateType(1, detail.getUserid());
				if (typeRs > 0) {
					result.put("code", 200);
					result.put("msg", "添加成功！");
					return result;
				}
				result.put("code", 200);
				result.put("msg", "添加成功！");
				return result;
			}
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
	
	/***
	 * 专业律师拒绝申请
	 * @param applyId 申请ID编号
	 * @return
	 */
	@RequestMapping(value = "refuse/{applyId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> refuse(@PathVariable(value = "applyId") int applyId) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = lawyerService.deleteById(applyId);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "已经拒绝！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
	
	/***
	 * 其他律师申请拒绝
	 * @param applyId 申请编号
	 * @return
	 */
	@RequestMapping(value = "refuse/student/{applyId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> refuseStu(@PathVariable(value = "applyId") int applyId) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = lawyerService.deleteStuById(applyId);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "已经拒绝！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
	
	/**
	 * 已审核专业律师冻结
	 * @param lawyerId 律师ID
	 * @return
	 */
	@RequestMapping(value = "freeze/{lawyerId}", method = RequestMethod.GET)
	public String freeze(@PathVariable(value = "lawyerId") int lawyerId) {
		int rs = lawyerService.updateState(1, lawyerId);
		if (rs > 0) {
			return "redirect:/lawyer/auditLawyer";
		}
		return "redirect:/lawyer/auditLawyer";
	}
	
	/***
	 * 已审核非专业律师冻结
	 * @param lawyerId 律师ID
	 * @return
	 */
	@RequestMapping(value = "student/freeze/{lawyerId}", method = RequestMethod.GET)
	public String freezeStu(@PathVariable(value = "lawyerId") int lawyerId) {
		int rs = lawyerService.updateState(1, lawyerId);
		if (rs > 0) {
			return "redirect:/lawyer/student/auditLawyer";
		}
		return "redirect:/lawyer/student/auditLawyer";
	}
	
	/**
	 * 已审核专业律师按条件查找
	 * @param serviceType 领域类型
	 * @param region 区域
	 * @param workYears 工作年限
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "search", method = RequestMethod.POST)
	public String searchLawyer(int serviceType, int region, int workYears,Model model) {
		List<TLawyerServiceType> serviceTypes = lawyerService.getServiceType();
		List<TProvince> provinces = lawyerService.getProvince();
		int begin = 0;
		int initYear = workYears;
		if (workYears == 1) {
			workYears = 4;
		}
		if (workYears == 2) {
			begin = 4;
			workYears = 9;
		}
		if (workYears == 3) {
			begin = 9;
			workYears = 15;
		}
		List<TLawyerDetailCustom> lawyerDetail = lawyerService.getSearchLawyer(serviceType, begin, workYears, region);
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		for (TLawyerDetailCustom lawyer : lawyerDetail) {
			lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
			lawyer.setSpecialNameB(specials.get(lawyer.getSpecialb()+""));
			lawyer.setSpecialNameC(specials.get(lawyer.getSpecialc()+""));
		}
		model.addAttribute("lawyers", lawyerDetail);
		model.addAttribute("serviceTypes", serviceTypes);
		model.addAttribute("provinces", provinces);
		model.addAttribute("serviceId", serviceType);
		model.addAttribute("region", region);
		model.addAttribute("workYears", initYear);
		return "lawyer/hire-lawyer";
	}
	
	/**
	 * 已审核非专业律师按条件搜索
	 * @param serviceType 领域类型
	 * @param region	地区
	 * @param workYears	年限
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "student/search", method = RequestMethod.POST)
	public String searchStu(int serviceType, int region, int workYears,Model model) {
		List<TLawyerServiceType> serviceTypes = lawyerService.getServiceType();
		List<TProvince> provinces = lawyerService.getProvince();
		int begin = 0;
		int initYear = workYears;
		if (workYears == 1) {
			workYears = 4;
		}
		if (workYears == 2) {
			begin = 4;
			workYears = 9;
		}
		if (workYears == 3) {
			begin = 9;
			workYears = 15;
		}
		List<TStudentDetaiCustom> lawyerDetail = lawyerService.getSearchStu(serviceType, begin, workYears, region);
		Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
		for (TStudentDetaiCustom lawyer : lawyerDetail) {
			lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
		}
		model.addAttribute("lawyers", lawyerDetail);
		model.addAttribute("serviceTypes", serviceTypes);
		model.addAttribute("provinces", provinces);
		model.addAttribute("serviceId", serviceType);
		model.addAttribute("region", region);
		model.addAttribute("workYears", initYear);
		return "lawyer/hire-stu";
	}
	
	/***
	 * 更新专业已审核律师
	 * @param detail 律师详情
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateLawyer(TLawyerDetailCustom detail) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = lawyerService.updateLawyer(detail);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "修改成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
	
	/***
	 * 更新非专业律师
	 * @param detail 律师详情
	 * @return
	 */
	@RequestMapping(value = "student/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateStu(TStudentDetaiCustom detail) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = lawyerService.updateStu(detail);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "修改成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
	
	/**
	 * 通过非专业律师申请
	 * @param detail
	 * @param applyId
	 * @return
	 */
	@RequestMapping(value = "student/agree", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> agreeStu(TStudentDetaiCustom detail, int applyId,HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		TLawyer lawyer = new TLawyer();
		lawyer.setName(detail.getName());
		lawyer.setSex(detail.getSex().charAt(0));
		lawyer.setState(0);
		lawyer.setType(1);
		lawyer.setUserId(detail.getUserid());
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		MultipartFile multipartFile = multipartRequest.getFile("file");
		//图片存储访问路径为 images
		String imgUrl = Upload.uploadFile(multipartFile, request, "images/",detail.getName());
		//这两个操作应该在一个事务中，这样写是有问题的，不在一个事务会出现错误
		int getIdRs = lawyerService.addAndGetId(lawyer);
		int delRs = lawyerService.deleteStuById(applyId);
		if (getIdRs > 0 && delRs > 0) {
			detail.setIdcardimg(imgUrl);
			detail.setStudentid(lawyer.getLawyerId());
			int rs = lawyerService.addStuLawyer(detail);
			if (rs > 0) {
				int typeRs = userService.updateType(2, detail.getUserid());
				if (typeRs > 0) {
					result.put("code", 200);
					result.put("msg", "添加成功！");
					return result;
				}
			}
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
}
